# -*- coding: utf-8 -*-
"""
Time    : 2024-4-16 15:16
ProjectName    : flask-pro-test
FileName    : gallery.py
"""
import uuid

from datetime import datetime
from apps import db


class BaseModel(object):
    """模型基类，为每个模型补充创建时间与更新时间"""

    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(
        db.DateTime, default=datetime.now, onupdate=datetime.now
    )  # 记录的更新时间


class Gallery(BaseModel, db.Model):
    """图片库"""
    __tablename__ = "i_gallery"
    id = db.Column(db.String(50), primary_key=True, comment="图片ID")
    name = db.Column(db.String(100), comment="图片名称")
    image = db.Column(db.LargeBinary, comment="图片数据")
    image_status = db.Column(db.INTEGER, default=1, comment="帐号状态（1正常 2禁用")

    def __init__(self, kwargs):
        self.id = str(uuid.uuid4())
        self.name = kwargs['name']
        self.image = kwargs['image'].encode("utf-8")  # 不设置编码，会报错
        self.image_status = int(1)

    def to_gallery_dict(self):
        """将图库基本信息转换为字典数据"""
        gallery_dict = {
            "id": self.id,
            "name": self.name,
            "image": self.image,
            "image_status": self.image_status
        }
        return gallery_dict

    def save_data(self):
        """保存数据"""
        db.session.add(self)
        db.session.commit()

    def __repr__(self):
        return f"<Gallery (id='{self.id}', filename='{self.filename}'...)>"

